<?php
namespace Modules\Ckeditor\Controllers;

use Core\Mvc\Controller;
use Modules\File\Models\File as FileModel;
use Modules\File\Library\FileHandle;
use Core\Paginator\Adapter\Collection as CollectionPaginator;

class IndexController extends Controller
{
    protected $root = '/';
    protected $imgext = ['bmp', 'gif', 'jpg', 'jpe', 'jpeg', 'png']; // allowed image extensions
    protected $imgdr = ''; // current folder (in $root) with images

    public function initialize()
    {
        if($this->request->hasPost('imgroot')){
            $this->root = $this->request->getPost('imgroot');
        }

        $this->root = trim($this->root, '/') . '/';
        $this->imgdr = $this->request->hasPost('imgdr') ? trim($this->request->getPost('imgdr'), '/') . '/' : '';
    }

    public function indexAction()
    {
        $getData = $this->request->getQuery();
        $one = preg_match_all('/([a-z]+)/', $getData['CKEditor']);
        if ($one > 1 || $one = 0) {
            return false;
        }
        $one = preg_match_all('/([0-9]+)/', $getData['CKEditorFuncNum']);
        if ($one > 1 || $one = 0) {
            return false;
        }
        $one = preg_match_all('/([a-z\-]+)/', $getData['CKEditorFuncNum']);
        if ($one > 1 || $one = 0) {
            return false;
        }
        self::$variables += array(
            '#templates' => 'ckBrowseImage',
            'CKEditor' => $getData['CKEditor'],
            'CKEditorFuncNum' => (int)$getData['CKEditorFuncNum'],
            'langCode' => $getData['langCode'],
        );
    }

    public function browseImageListAction($page)
    {
        if(!$this->user->isLogin()){
            return $this->notFount();
        }
        $query = [
            'conditions' => [
                'extension' => [
                    '$in' => [
                        'jpeg','png','jpg','gif'
                    ]
                ]
            ],
            'sort' => [
                'changed' => -1
            ],
            'limit' => 20,
            'skip' => 20 * ($page - 1),
        ];
        $data = FileModel::find($query);
        $paginator = new CollectionPaginator([
            'data' => $data,
            'query' => $query,
            'page' => $page,
            'collection' => '\Modules\File\Models\File',
        ]);
        self::$variables += array(
            '#templates' => 'ckBrowseImageList',
            'data' => $paginator->getPaginate(),
            'page' => $page,
        );
    }

    public function uploadImageAction()
    {
        $data = '';
        if ($this->request->hasFiles() == true) {
            $output = FileHandle::upload(array(), 'upload');
            if (!empty($output['success'])) {
                $upimg = end($output['success']);
                $callback = $this->request->get('CKEditorFuncNum');
                $data .= '<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction(' . $callback . ',"' . $upimg['url'] . '","");</script>';
            } else {
                $data .= '<script type="text/javascript">alert("上传图片失败啦")</script>';
            }
        }else{
            $data .= '<script type="text/javascript">alert("上传图片失败啦")</script>';
        }

        self::$variables += array(
            '#templates' => 'upload_image',
            'data' => &$data
        );
    }
}
